{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "int16\n",
      "int64\n",
      "8\n",
      "0.72\n",
      "[0.07827817 0.14606297 0.04930004 0.84893357 0.65459169 0.18822098\n",
      " 0.93474407 0.64496921 0.39761425 0.80339605]\n",
      "8\n",
      "float64\n",
      "[0.07827817 0.14606297 0.04930004 0.8489336  0.6545917  0.18822098\n",
      " 0.93474406 0.6449692  0.39761424 0.80339605]\n",
      "[0.08 0.15 0.05 0.85 0.65 0.19 0.93 0.64 0.4  0.8 ]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#指定dtype的目的是减少内存的使用，提高运算效率\n",
    "#np.int8到np.int64\n",
    "import random\n",
    "f = np.array([1,2,3,4,5], dtype = np.int16) # 返回数组中每个元素的字节单位长度,dtype设置数据类型\n",
    "print(f.itemsize) # 1 np.int8(一个字节)\n",
    "# 获取数据类型\n",
    "print(f.dtype)\n",
    "\n",
    "# 调整数据类型\n",
    "f1 = f.astype(np.int64)\n",
    "print(f1.dtype)\n",
    "print(f1.itemsize)\n",
    "\n",
    "# 拓展随机生成小数\n",
    "# 使用python语法，round是保留多少位\n",
    "print(round(random.random(),2))\n",
    "\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "print(arr)\n",
    "print(arr.itemsize)\n",
    "print(arr.dtype)\n",
    "arr1=arr.astype(np.float32)\n",
    "print(arr1)\n",
    "#float32精度为6-7位\n",
    "#double(float64)精度是15-16位\n",
    "\n",
    "# 取小数点后两位\n",
    "print(np.round(arr,2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}